package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String NURSINGPLAN_ALL = "nursingplan:all";

    public void deleteCache() {
        //删除缓存
        redisTemplate.delete(NURSINGPLAN_ALL);
    }
/*    *//**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     *//*
    @Override
    public NursingPlan selectNursingPlanById(Long id)
    {
        return getById(id);
    }*/
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //1.查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        //2.查询中间表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.数据封装
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(nursingProjectPlanVos);

        return vo;
    }
    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
/*    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        return save(nursingPlan) ? 1 : 0;
    }*/
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        //1.新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        boolean saveResult = this.save(nursingPlan);

        if (saveResult) {
            deleteCache(); // 清除缓存
        }
        //2.新增护理计划项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1 : 0;
    }
    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        boolean result = updateById(nursingPlan);
        if (result) {
            deleteCache(); // 更新成功后清除缓存
        }
        return result ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean result = removeByIds(Arrays.asList(ids));
        if (result) {
            deleteCache(); // 删除成功后清除缓存
        }
        return result ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean result = removeById(id);
        if (result) {
            deleteCache(); // 删除成功后清除缓存
        }
        return result ? 1 : 0;
    }
}
